import { Message } from 'element-ui'
import request from '@/utils/request'

const isResponseSuccess = (response) => {
  return response && response.code === 0;
}

export const purchaseRequest = async (options) => {
  const { url, type, data = {}, isFormData, isExport, loadingFn } = options;
  const config = {
    url,
    method: type,
    data,
    isExport,
    headers: {
      'Content-Type': isFormData ? 'application/x-www-form-urlencoded' : 'application/json;charset=UTF-8',
    },
    baseURL: '/purchase/'
  }
  if (loadingFn) loadingFn(true)
  try {
    const res = await request(config);
    if (loadingFn)loadingFn(false)
    if (isExport) {
      return res;
    }
    if (isResponseSuccess(res)) {
      return res;
    } else {
      Message.error(res.message)
    }
  } catch (err) {
    if (loadingFn)loadingFn(false)
  }
}
